<?php

namespace ua_backend\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use ua_backend\models\Shops;

/**
 * RadoLocationsSearch represents the model behind the search form of `backend\models\RadoLocations`.
 */
class ShopsSearch extends Shops
{
    public $skey;
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id'], 'integer'],
            [['code', 'uuid', 'shop', 'address', 'role'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Shops::find()->where(['<>','role','sys']);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pagesize' => Yii::$app->params['pageSize'],
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
//        $query->andFilterWhere([
//            'id' => $this->id,
//            'code' => $this->code,
//        ]);
//
//        $query->andFilterWhere(['like', 'shop', $this->shop])
//            ->andFilterWhere(['like', 'address', $this->address])
//            ->andFilterWhere(['like', 'uuid', $this->uuid]);

        if(isset(Yii::$app->request->get('ShopsSearch')['skey'])){
            $skey = Yii::$app->request->get('ShopsSearch')['skey'];
            $query->andFilterWhere(['like', 'uuid',  $skey]);
            $query->orFilterWhere(['like', 'shop',  $skey]);
            $query->orFilterWhere(['like', 'code',  $skey]);
        }

        return $dataProvider;
    }


    public function filter($params)
    {
        $query = Shops::find()->where(['<>','role','sys']);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pagesize' => Yii::$app->params['pageSize'],
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'code' => $this->code,
        ]);

        $query->andFilterWhere(['like', 'shop', $this->shop])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'uuid', $this->uuid]);


        return $dataProvider;
    }
}
